<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        /* 
            实现一个电子钟：都是汉字中文 显示年月日 时分秒

         1.10-20 之间 十一 、 十二、十三 。。。。

        2. 20 以后 二十一 二十二 二十三 ....

        3.20,30,40 50... 二十 三十 四十

        4.不需要考虑 60以上 
        
        */

        // 0,1,2,3....60;
        // 2021--->二零二一

        /* 
            {
                0:"零",
                1:"一"
            }
        
        
        
        
        */


        // num的值 是0到60之间
        function getWord(num) {
            var arr = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"];
            if (0 <= num && num <= 10) {
                return arr[num];
            } else if (10 < num && num < 20) {
                num = num + "";
                return "十" + arr[num[1]];
            } else if (num > 20 && num % 10 != 0) {
                num = num + "";
                return arr[num[0]] + "十" + arr[num[1]];
            } else if (num >= 20 && num % 10 == 0) {
                num = num + "";
                return arr[num[0]] + "十";
            } else {
                throw new Error("输入错误");
            }
        }

        // var res = getWord(40);
        // console.log(res);
        function getTimeFn() {
            var time = new Date();
            var year = fomartYear(time.getFullYear());
            // console.log(year);
            var month = getWord(time.getMonth() + 1);
            var day = getWord(time.getDate());
            var hour = getWord(time.getHours());
            var minute = getWord(time.getMinutes());
            var second = getWord(time.getSeconds());
            var str = `显示时间是${year}年${month}月${day}日 ${hour}时 ${minute}分 ${second}秒`;
            document.body.innerHTML = str;
        }
        getTimeFn();
        setInterval(function () {
            getTimeFn();
        }, 1000)





        // 把年份转换成中文的年份；
        function fomartYear(year) {
            year = year + "";
            var str = "";
            for (var i = 0; i < year.length; i++) {
                str += getWord(year[i]);
            }
            return str;
        }

        // console.log( fomartYear(2021));


        // 要求 如果数据里的children 是空的 那么就删除children 
        var obj = [{
            name: "111",
            children: []
        }, {
            name: "222",
            children: [{
                name: "333",
                children: []
            },
            {
                name: "444",
                children: []
            }]
        }, {
            name: "555",
            children: [{
                name: "666",
                children: [{name:"777",children:[]}]
            }]
        }]
        formartData(obj);
        function formartData(data) {
            data.forEach(function(item,key){
                if(item.children.length==0){
                    delete data[key].children;
                }else{
                    formartData(data[key].children);
                }
            })
            console.log(data);
        }


        // var arr = [];
        // console.log(arr.length);
        // var obj = {
        //     name:"111",
        //     children:[]
        // }
        // if(obj.children.length==0){
        //     delete obj.children;
        // }
        // console.log(obj);

    </script>
</body>

</html>